package com.zh.api.controller.family;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.IllDO;
import com.zh.familyInfo.service.IllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * @author 马广洋
 * @email 1992lcg@163.com
 * @date 2019-05-25 10:54:07
 */
@Controller
@Api("老人疾病信息【既往病史控制层】")
public class IllController extends BaseController {
	@Autowired
	private IllService illService;


	@Log("显示老人既往病史所有信息")
	@ResponseBody
	@RequestMapping(value = "listFamilyIll",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO listFamilyIll(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
        Query query = new Query(params);
		List<IllDO> illList = illService.list(query);
		int total = illService.count(query);
		PageUtils pageUtils = new PageUtils(illList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑个人既往病史信息")
	@ResponseBody
	@RequestMapping(value = "editFamilyIllById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public  ResultVO editFamilyIllById(@RequestBody IllDO ill){
		if (ill==null|| CheckEmptyUtil.isEmpty(ill.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		IllDO ills = illService.get(ill.getId());
	    return ResultVO.success(ills);
	}

	@Log("新增老人既往病史信息")
	@ResponseBody
	@RequestMapping(value = "saveFamilyIll",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO saveFamilyIll(@RequestBody IllDO ill){
		if (ill==null) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(illService.save(ill)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("更新老人既往病史信息")
	@ResponseBody
	@RequestMapping(value = "updateFamilyIllById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO updateFamilyIllById(@RequestBody IllDO ill){
		if (ill==null|| CheckEmptyUtil.isEmpty(ill.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(illService.update(ill)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("删除老人既往病史信息")
	@ResponseBody
	@RequestMapping(value = "removeIllById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO removeIllById(@RequestBody IllDO ill){
		if (ill==null|| CheckEmptyUtil.isEmpty(ill.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(illService.remove(ill.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
    @RequestMapping(value = "batchRemoveFamilyIllByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除老人既往病史信息")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
    	if(jsonParam==null){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	List<String> ids= (List<String>)jsonParam.get("ids");
    	if(CheckEmptyUtil.isEmpty(ids)){
    		return ResultVO.error(ResultEnum.PARAME_ERROR);
    	}
    	String[] strs1=ids.toArray(new String[ids.size()]);
    	if(illService.batchRemove(strs1)>0){
    		return ResultVO.success();
    	}
	  return ResultVO.error();
    }
}
